"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.decryptUTF8 = exports.decrypt = exports.strToArray = exports.filp = void 0;
const utils_1 = require("../utils");
function strToArray(str) {
    let result = [];
    str.split("").forEach((v, k) => (result[k] = v));
    return result;
}
exports.strToArray = strToArray;
function filp(arr) {
    let filpedArr = {};
    Object.keys(arr).forEach(v => (filpedArr[arr[v]] = v));
    return filpedArr;
}
exports.filp = filp;
function decrypt(str, solution) {
    let plain = "";
    const decryptKey = strToArray(solution);
    const filpDecryptKey = filp(decryptKey);
    str.split("").forEach((v, k) => {
        let index = Number(filpDecryptKey[v]) - k;
        while (index <= -1 || index >= utils_1.configKey.getKey().length + 1) {
            if (index <= -1) {
                index = index + utils_1.configKey.getKey().length;
            }
            if (index >= utils_1.configKey.getKey().length + 1) {
                index = index - utils_1.configKey.getKey().length;
            }
        }
        plain = plain.concat(utils_1.configKey.getKey()[index]);
    });
    return plain;
}
exports.decrypt = decrypt;
function decryptUTF8(cipher, solution, callback) {
    try {
        return decodeURI(decrypt(cipher, solution));
    }
    catch (error) {
        callback && callback(error);
    }
}
exports.decryptUTF8 = decryptUTF8;
